﻿namespace HRM.Update.Process
{
    using APP.HELPER.Utils;
    using HRM.Update.DS;
    using System;
    using System.Data;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    using System.Xml.XPath;

    public class CheckUpdate
    {
        private static string string_0;
        private static string string_1;

        public static unsafe bool CheckUpdateVersion()
        {
            string_0 = AssemblyHelper.AssemblyVersion;
            string_1 = ReadUpdateVersion();
            bool ischeck = false;
            int check = string.Compare(string_1, string_0, true);
            if (check == 0)
            {
                check = 1;
                ischeck = true;
            }
            else
            {
                check = 0;
                ischeck = false;
            }
            return ischeck;
        }

        public static string ReadUpdateVersion()
        {
            string str = "";
            string xpath = "Update/Application[@ID='" + Class0.smethod_2() + "']/NewUpdate[@ID='" + Class0.smethod_3() + "']";
            try
            {
                HttpWebRequest request = WebRequest.Create("http://update.fessoft.com/update.xml") as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader textReader = new StreamReader(response.GetResponseStream());
                    XPathDocument document = new XPathDocument(textReader);
                    XPathNodeIterator iterator = document.CreateNavigator().Select(xpath);
                    while (iterator.MoveNext())
                    {
                        foreach (XPathNavigator navigator2 in iterator.Current.SelectChildren(XPathNodeType.Element))
                        {
                            string name = navigator2.Name;
                            if ((name != null) && (name == "Version"))
                            {
                                str = navigator2.Value;
                            }
                        }
                        iterator.Current.MoveToFirstAttribute();
                    }
                }
            }
            catch
            {
                str = "";
            }
            return str;
        }

        public static string CurrentVersion
        {
            get
            {
                return string_0;
            }
            set
            {
                string_0 = value;
            }
        }
        public static string RequestVersion
        {
            get
            {
                return string_1;
            }
            set
            {
                string_1 = value;
            }
        }
    }

    internal class Class0
    {
        public static unsafe string smethod_0()
        {
            dsUpdateOnline online = new dsUpdateOnline();
            int rowCount = 0;
            bool fileExist = !File.Exists(Application.StartupPath + @"\Update\UpdateConfig.xml");
            if (fileExist)
            {
                online.UpdateConfig.ReadXml(Application.StartupPath + @"\Update\UpdateConfig.xml");
                rowCount = online.UpdateConfig.Rows.Count;
            }
            return online.UpdateConfig.Rows[rowCount - 1]["Version"].ToString();
        }

        public static unsafe string smethod_1()
        {
            dsUpdateOnline online = new dsUpdateOnline();
            int rowCount = 0;
            bool fileExist = !File.Exists(Application.StartupPath + @"\Update\UpdateConfig.xml");
            if (fileExist)
            {
                online.UpdateConfig.ReadXml(Application.StartupPath + @"\Update\UpdateConfig.xml");
                rowCount = online.UpdateConfig.Rows.Count;
            }
            return online.UpdateConfig.Rows[rowCount - 1]["OutFile"].ToString();
        }

        public static unsafe string smethod_2()
        {
            dsUpdateOnline online = new dsUpdateOnline();
            int rowCount = 0;
            bool fileExist = !File.Exists(Application.StartupPath + @"\Update\UpdateConfig.xml");
            if (fileExist)
            {
                online.UpdateConfig.ReadXml(Application.StartupPath + @"\Update\UpdateConfig.xml");
                rowCount = online.UpdateConfig.Rows.Count;
            }
            return online.UpdateConfig.Rows[rowCount - 1]["UpdateType"].ToString();
        }

        public static unsafe string smethod_3()
        {
            dsUpdateOnline online = new dsUpdateOnline();
            int rowCount = 0;
            bool FileExist = !File.Exists(Application.StartupPath + @"\Update\UpdateConfig.xml");
            if (FileExist)
            {
                online.UpdateConfig.ReadXml(Application.StartupPath + @"\Update\UpdateConfig.xml");
                rowCount = online.UpdateConfig.Rows.Count;
            }
            return online.UpdateConfig.Rows[rowCount - 1]["Customer"].ToString();
        }

        public static void funcupdateFile1(UpdateFile updateFile_0)
        {
            try
            {
                dsUpdateOnline online = new dsUpdateOnline();
                string path = Application.StartupPath + @"\Update\UpdateConfig.xml";
                if (File.Exists(path))
                {
                    online.UpdateConfig.ReadXml(path);
                }
                online.UpdateConfig.Rows[0]["Version"] = updateFile_0.Version;
                online.WriteXml(path, XmlWriteMode.IgnoreSchema);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Message: " + exception.Message);
                Application.ExitThread();
                Application.Exit();
            }
        }
        public static void funcupdateFile2(UpdateFile updateFile_0)
        {
            dsUpdateOnline online = new dsUpdateOnline();
            string path = Application.StartupPath + @"\Update\UpdateFile.xml";
            if (File.Exists(path))
            {
                online.UpdateFile.ReadXml(path);
            }
            online.UpdateFile.AddUpdateFileRow(updateFile_0.Version, updateFile_0.ZipName, DateTime.Now.ToString("dd/MM/yyyy"));
            online.WriteXml(path, XmlWriteMode.IgnoreSchema);
        }
    }
}

